﻿using System;
using System.Data.SqlClient;
using System.Web.UI.WebControls;
using design.WorkWithDataClasses;

namespace design
{
    public partial class Article : PageTemplate
    {
		protected override void Page_Load(object sender, EventArgs e)
		{
			site = "Article.aspx";
			base.Page_Load(sender, e);
		}

		protected override int GetItemsCount(SqlConnection connection)
		{
			string cmdText = "select count(*) as c from articles";
			SqlCommand cmd = new SqlCommand(cmdText, connection);
			SqlDataReader reader = cmd.ExecuteReader();
			reader.Read();

			int count = Convert.ToInt32(reader["c"]);
			reader.Close();

			return count;
		}

		protected override string GetSQLStr(int lowerBorder, int upperBorder)
		{
			string cmdText = "select * ";
			cmdText += "from ( ";
			cmdText += "select row_number() over( ";
			cmdText += "order by datepub desc) as rownum, ";
			cmdText += "articles.id, articles.datepub, articles.title, articles.short_text, ";
			cmdText += "articles.full_text, articles.id_media, ";
			cmdText += "medias.path as mpath ";
// а разве автор тут не нужен?
			//cmdText += "authors.lastname as alast ";
			cmdText += "from articles, medias ";
			cmdText += "where articles.id_media = medias.id ) ";
			cmdText += "as b ";
			cmdText += "where rownum between " + lowerBorder + " and " + upperBorder;

			return cmdText;
		}

		protected override void AddControls(ContentPlaceHolder content, int i, SqlDataReader reader, bool head)
		{
			sArticle article = new sArticle(Convert.ToInt32(reader["id"]), Convert.ToInt32(reader["id_media"]),
				reader["short_text"].ToString(), reader["full_text"].ToString(),
				reader["datepub"].ToString(), reader["title"].ToString());

			string href;
			if (head)
			{
				href = site + "?currentItem=" + randomItem;
			}
			else
			{
				href = site + "?currentItem=" + (itemsOnPage * (currentPage - 1) + i);
			}

			Literal contentLiteral = new Literal();
			contentLiteral.ID = "contentLiteral_ID_" + i;
			contentLiteral.Text = "<br/><a href='" + href + "' ><span class='itemTitle'>" + article.Title + "</span></a>";
			contentLiteral.Text += "<div class='Container'>";
			contentLiteral.Text += "<a href='" + href + "' ><img class='mainImg' src='" + reader["mpath"] + "' alt='mainImg' /></a>";
			contentLiteral.Text += "<div class='info'>";
			contentLiteral.Text += "<p> <br/> <span style='text-decoration: underline;'>Информация о статье:</span><br/>";
			contentLiteral.Text += "<b>Название:</b>" + article.Title + "<br/>";
			//contentLiteral.Text += "<b>Автор:</b>" + reader["alast"] + "<br/>";
			contentLiteral.Text += "<b>Дата публикации:</b>" + article.Date + "<br/>";
			contentLiteral.Text += "<b>Описание:</b><br/>" + article.ShortText + " </p>";
			// описание слишком короткое (255 символов)
			//contentLiteral.Text += "<p><a href='" + book.Path + "'>Скачать</a> </p>";

			contentLiteral.Text += "</div></div><br/>";
			content.Controls.Add(contentLiteral);
			//content.Controls.Add(this.CreateButton(i, book.Path));

			if (!head)
			{
				Literal endItemLiteral = new Literal();
				endItemLiteral.ID = "endItemLiteral_ID_" + i;
				endItemLiteral.Text += "<div style='background-image: url(../images/dotted-lines.png); background-repeat: repeat-x; height: 6px;'></div><br/>";
				content.Controls.Add(endItemLiteral);
			}
		}
    }
}